$(function () {
  var form = layui.form;
  var layer = layui.layer;
  form.verify({
    password: [/^[\S]{6,12}$/, "密码必须6到12位，且不能出现空格"],
    samepass: function (item) {
      if (item === $("[name=oldPwd]").val()) return "新密码与旧密码不能相同";
    },
    confirm: function (item) {
      if (item !== $("[name=newPwd]").val()) return "两次密码不一致";
    },
  });
  $(".layui-form").on("submit", function (e) {
    e.preventDefault();
    $.ajax({
      type: "POST",
      url: "/my/updatepwd",
      data: $(this).serialize(),
      success: function (res) {
        if (res.status !== 0) return layer.msg(res.message);
        layer.msg(res.message);
        // 清空表单
        $(".layui-form")[0].reset();
        layer.alert("密码修改成功，请重新登录", function () {
          localStorage.removeItem("token");
          window.parent.location.href = "/login.html";
        });
      },
    });
  });
});
